/*
@title: Chess Puzzle
@description: Chess Puzzle is a strategic puzzle game where you control a chess knight, maneuvering it around a board filled with obstacles to reach a goal marked by a pink and purple box.
@author: Silver Canvas
@tags: ["puzzle", "strategy"]
@addedOn: 2024-06-25
First time? Check out the tutorial game:
https://sprig.hackclub.com/gallery/getting_started

*/

const rook = "r"
const bishop = "b"
const queen = "q"
const knight = "n"
const cursor = "c"
const tile = "t"
const wallw = 'w'
const wallb = 'a'
const goal = "g"
const BackgroundTune = tune`
256.4102564102564,
256.4102564102564: C4^256.4102564102564 + E4^256.4102564102564 + A4~256.4102564102564 + B4~256.4102564102564 + C5~256.4102564102564,
256.4102564102564: C4^256.4102564102564 + D5~256.4102564102564,
256.4102564102564: E5~256.4102564102564 + C5~256.4102564102564 + D5~256.4102564102564,
256.4102564102564: E4^256.4102564102564 + C4^256.4102564102564 + E5~256.4102564102564 + B4~256.4102564102564,
256.4102564102564: C4^256.4102564102564 + E5~256.4102564102564 + A4~256.4102564102564 + B4~256.4102564102564,
256.4102564102564: E5~256.4102564102564 + D5~256.4102564102564,
256.4102564102564: E4^256.4102564102564 + C4^256.4102564102564 + D5~256.4102564102564,
256.4102564102564: C4^256.4102564102564 + D5~256.4102564102564 + C5~256.4102564102564 + B4~256.4102564102564,
256.4102564102564: B4~256.4102564102564 + A4~256.4102564102564,
256.4102564102564: E4^256.4102564102564 + C4^256.4102564102564 + A4~256.4102564102564,
256.4102564102564: C4^256.4102564102564 + A4~256.4102564102564 + G4~256.4102564102564 + C5~256.4102564102564,
256.4102564102564: G4~256.4102564102564 + C5~256.4102564102564 + D5~256.4102564102564 + E5~256.4102564102564,
256.4102564102564: E4^256.4102564102564 + C4^256.4102564102564 + G4~256.4102564102564 + A4~256.4102564102564 + B4~256.4102564102564,
256.4102564102564: C4^256.4102564102564 + E5~256.4102564102564,
256.4102564102564: E5~256.4102564102564,
256.4102564102564: E4^256.4102564102564 + C4^256.4102564102564 + E5~256.4102564102564 + D5~256.4102564102564 + C5~256.4102564102564,
256.4102564102564: C4^256.4102564102564 + F4~256.4102564102564 + G4~256.4102564102564,
256.4102564102564: G4~256.4102564102564 + A4~256.4102564102564,
256.4102564102564: E4^256.4102564102564 + C4^256.4102564102564 + A4~256.4102564102564 + B4~256.4102564102564 + D5~256.4102564102564,
256.4102564102564: C4^256.4102564102564 + B4~256.4102564102564 + C5~256.4102564102564,
256.4102564102564: D5~256.4102564102564 + E5~256.4102564102564 + B4~256.4102564102564 + A4~256.4102564102564,
256.4102564102564: E4^256.4102564102564 + C4^256.4102564102564 + E5~256.4102564102564 + A4~256.4102564102564 + B4~256.4102564102564,
256.4102564102564: C4^256.4102564102564 + D5~256.4102564102564 + C5~256.4102564102564 + A4~256.4102564102564,
256.4102564102564: C5~256.4102564102564 + B4~256.4102564102564 + A4~256.4102564102564 + G4~256.4102564102564,
256.4102564102564: E4^256.4102564102564 + C4^256.4102564102564 + A4~256.4102564102564 + G4~256.4102564102564 + D5~256.4102564102564,
256.4102564102564: C4^256.4102564102564 + E5~256.4102564102564 + F5~256.4102564102564,
256.4102564102564: F5~256.4102564102564,
256.4102564102564: E4^256.4102564102564 + C4^256.4102564102564 + F5~256.4102564102564 + E5~256.4102564102564,
256.4102564102564: C4^256.4102564102564 + E5~256.4102564102564 + D5~256.4102564102564 + C5~256.4102564102564,
256.4102564102564: B4~256.4102564102564 + A4~256.4102564102564,
256.4102564102564: E4^256.4102564102564 + A4~256.4102564102564`
const dieSound = tune`
500,
500: G4-500 + B4-500 + A4/500,
15000`

playTune(BackgroundTune, Infinity)

let player = knight

addText("WASD To Move Cursor", { x: 1, y: 7, color: color`H` })
addText("'J' to Move Peice", { x: 2, y: 10, color: color`5` })

setLegend(
  [rook, bitmap`
................
.001..LLL1..LL1.
.011..LL11..L11.
.0LLLLLLLLLLLL1.
.00LLLLLLLLLL11.
..0000LLLLLLL1..
.....0LLLLL.....
....00LLLLLL....
.....0LLLLL.....
.....0LLL2L.....
....00LLL2LL....
...0LLLLLL2LL...
.00LLLLLLLL2LLL.
..0LLLLLLLL2LL..
.0LLLLLLLLLLLLL.
................`],
  [bishop, bitmap`
................
.......01.......
......0LL1......
.......01.......
.....00L2L......
....0LL2L..1....
....0LLL..L1....
....0LLL.LL1....
.....00LLL1.....
......0L21......
.....00L211.....
......0L21......
.....00L211.....
....000L2L11....
....00LLLLL1....
................`],
  [queen, bitmap`
................
................
..01...01...01..
.0L11.0L11.0L11.
..01...L1...L1..
...0LLLLLLLL1...
...0LLLLLL21L...
....00LLL21L....
.....00LL2L.....
......0L2L......
.....00L2LL.....
......0L2L......
.....00L2L1.....
....000L2L11....
....00LLLLL1....
................`],
  [knight, bitmap`
................
....0...L.......
....0LLLLL......
...1LLLL10LL....
..1LLLLL100LL...
..0LLLL0LLLL2L..
..1LLLLL0LL22L..
..0LLLLL0.LLL...
..1LLLLL0.......
..0LLLLLL.......
...1LLLLLLLL....
....0LLLL22LL...
....0LLLLL2LL...
...00LLLLLL2L...
..00LLLLLLLLLL..
................`],
  [cursor, bitmap`
33333......33333
33............33
3..............3
3..............3
3..............3
................
................
................
................
................
................
3..............3
3..............3
3..............3
33............33
33333......33333`],
  [tile, bitmap`
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777`],
  [wallw, bitmap`
2200000000000022
2000000000000002
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
2000000000000002
2200000000000022`],
  [wallb, bitmap`
7700000000000077
7000000000000007
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
7000000000000007
7700000000000077`],
  [goal, bitmap`
HH888HHH888HHH88
H888HHH888HHH888
888HHH888HHH888H
88HHH888HHH888HH
8HHH888HHH888HHH
HHH888HHH888HHH8
HH888HHH888HHH88
H888HHH888HHH888
888HHH888HHH888H
88HHH888HHH888HH
8HHH888HHH888HHH
HHH888HHH888HHH8
HH888HHH888HHH88
H888HHH888HHH888
888HHH888HHH888H
88HHH888HHH888HH`],


)

setSolids([wallw, wallb])

let level = 0
const levels = [
  map`
nt.t.t
tctwt.
.t.t.t
t.t.g.
.twt.t`,
  map`
nt.t.
tct.t
.a.t.
t.t.t
.tgt.`,
  map`
ntct
tgt.
.t.t`,
  map`
t.t.t
.tnt.
act.a
.tgt.`,
  map`
nt.
tca
gt.
t.a
.t.`,
  map`
.t.t.t
t.a.t.
.aca.t
ant.a.
.tgt.t
a.t.t.
.t.a.t`,
  map`
t.t.t.t.
.aca.a.a
ana.a.ag`,
  map`
t.t
ct.
gnt`,
  map`
.t.t.
twtwt
.tct.
t.g.t
.tnt.`,
  map`
.g.a.a
t.t.a.
.twtna
t.aca.
.t.t.t`,
]

setMap(levels[level])

//setPushables({
//[]: []
//})

onInput("s", () => {
  getFirst(cursor).y += 1
})

onInput("w", () => {
  getFirst(cursor).y -= 1
})

onInput("a", () => {
  getFirst(cursor).x -= 1
})

onInput("d", () => {
  getFirst(cursor).x += 1
})

onInput("j", () => {
  let cx = getFirst(cursor).x;
  let cy = getFirst(cursor).y;
  let px = getFirst(player).x;
  let py = getFirst(player).y;


  if ((Math.abs(px - cx) == 2) && (Math.abs(py - cy) == 1)) {
    getFirst(player).x = cx;
    getFirst(player).y = cy;
  } else if ((Math.abs(px - cx) == 1) && (Math.abs(py - cy) == 2)) {
    getFirst(player).x = cx;
    getFirst(player).y = cy;
  }
});




afterInput(() => {
  clearText()
  if (tilesWith(player, wallw).length >= 1) {
    playTune(dieSound)
    setMap(levels[level])
  } else if (tilesWith(player, wallb).length >= 1) {
    playTune(dieSound)
    setMap(levels[level])
  }
  if (tilesWith(player, goal).length >= 1) {
    level += 1
    if (level == 10) {
      addText("Congratulations!", { x: 2, y: 5, color: color`8` })
      addText("You're Done!", { x: 4, y: 8, color: color`H` })
    } else {
      setMap(levels[level])
    }
  }
})
